/*
 * Class name: 			ShoppingItem
 * Class description: 	The ShoppingItem class is the container of
 * 						all basic information about the shopping items in the global
 * 						list as well as individual shopping items each groups' list
 * Project:				GrocerWE
 */

package com.cse210.grocerwe.common;

import com.cse210.grocerwe.client.SItem;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


@PersistenceCapable(detachable="true")
public class ShoppingItem {

	public static int ST_NORMAL = 0;
	public static int ST_CURR_SHOPPING = 1;
	public static int ST_BOUGHT = 2;
	
	//database primary key
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    /*
     * Information fields 
     */
    
    //Name of Item
    @Persistent
	private String name;
    
    //Type/Category of item
    @Persistent
	private String type;
    
    //Brand
    @Persistent
    private String brand;
   
    //Quantity of item
    @Persistent
    private int quantity;
    
    //Quantity descriptor size/type
    private String quantity_size;
    
    //Description of item
    @Persistent
    private String description;
    
    /*
     * Back end fields
     */
    
    //Foreign Key, Person who added the item to the list
    @Persistent
    private String owner;
    
    //Foreign Key, Person who is shopping/has bought, the item
    @Persistent
    private String buyer;
    
    //group/list owning this item
    @Persistent
    private String group;
    
    //boolean decided whether this item is part of the global inventory
    //or someones specific list item
    @Persistent
	private boolean global_list;
    
    //status of item
    // 0 = normal, not bought, not in the process of shopping
    // 1 = Shopping, this item is currently being shopped for
    // 2 = Bought, this item has been bought by "buyer"
    @Persistent
    private int status;
    
    /*
     * Constructors
     */
	public ShoppingItem (String name, String type)
	{
		this.name = name;
		this.type = type;
		this.brand = null;
		this.quantity = 0;
		this.quantity_size = null;
		this.description = null;
		this.owner = null;
		this.buyer = null;
		this.group = null;
		this.global_list = true;
		this.status = ST_NORMAL;
	}
	public ShoppingItem (String name, String type, String owner, String group)
	{
		this.name = name;
		this.type = type;
		this.brand = null;
		this.quantity = 0;
		this.quantity_size = null;
		this.description = null;
		this.owner = owner;
		this.buyer = null;
		this.group = group;
		this.global_list = false;
		this.status = ST_NORMAL;
	}
	//Constructor used by Android interface
	public ShoppingItem (SItem s)
	{
		if(s.getKey() != null)
			this.key = KeyFactory.stringToKey(s.getKey());
		this.name = s.getName();
		this.type = s.getType();
		this.brand = s.getBrand();
		this.quantity = s.getQuantity();
		this.quantity_size = s.getQuantitySize();
		this.description = s.getDescription();
		this.owner = s.getOwner();
		this.buyer = s.getBuyer();
		this.group = s.getGroup();
		this.global_list = s.isGlobal();
		this.status = s.getStatus();
	}
	
	/*
	 * Getters
	 */
	public String getKey()
	{
		return KeyFactory.keyToString(key); 
	}
	public String getName()
	{
		return name;
	}
	public String getType()
	{
		return type;
	}
	public String getBrand()
	{
		return brand;
	}
	public int getQuantity()
	{
		return quantity;
	}
	public String getQuantitySize()
	{
		return quantity_size;
	}
	public String getDescription()
	{
		return description;
	}
	public String getOwner()
	{
		return owner;
	}
	public String getBuyer()
	{
		return buyer;
	}
	public String getGroup()
	{
		return group;
	}
	public boolean isGlobal()
	{
		return global_list;
	}
	public int getStatus()
	{
		return status;
	}
	
	/*
	 * Setters
	 */
	public void setName(String name)
	{
		this.name = name;
	}
	public void setType(String type)
	{
		this.type = type;
	}
	public void setBrand(String brand)
	{
		this.brand = brand;
	}
	public void setQuantity(int quantity)
	{
		this.quantity = quantity;
	}
	public void setQuantitySize(String quantity_size)
	{
		this.quantity_size = quantity_size;
	}
	public void setDescription(String description)
	{
		this.description = description;
	}
	public void setOwner(String owner)
	{
		this.owner = owner;
	}
	public void setBuyer(String buyer)
	{
		this.buyer = buyer;
	}
	public void setGroup(String group)
	{
		this.group = group;
	}
	public void setStatus(int status)
	{
		this.status = status;
	}
	
	/*
	 * Android functions
	 */
	public SItem toSItem()
	{
		return new SItem(KeyFactory.keyToString(key), name, type, brand, quantity, quantity_size,
				  description, owner, buyer, group, global_list,status);
	}
	
	public boolean isLocked()
	{
		return status != ST_NORMAL;
	}
	
	public synchronized boolean lock(String user)
	{
		if(isLocked())
			return false;
		else
		{
			status = ST_CURR_SHOPPING;
			this.buyer = user;
			return true;
		}
	}
	
	public synchronized boolean unlock(String user)
	{
		if(buyer.equals(user) && this.status != ST_BOUGHT)
		{
			this.buyer = null;
			this.status = ST_NORMAL;
			return true;
		}
		else
			return false;
	}
	
	public boolean bought(String user)
	{
		if(buyer.equals(user))
		{
			this.status = ST_BOUGHT;
			return true;
		}
		else
			return false;
	}
	
}